from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
from meiduo_admin.my_paginate import MyPageNumberPagination
from orders.models import OrderInfo
from . import order_serializers
class OrderViewSet(ModelViewSet):
    pagination_class = MyPageNumberPagination
    serializer_class = order_serializers.OrderViewSetSerializer
    # queryset = OrderInfo.objects.all()
    # 1 重写queryset方法
    def get_queryset(self):

        # 1，获取参数
        keyword = self.request.query_params.get('keyword','')

        # 2, 返回结果
        # print(OrderInfo.objects.filter(order_id__contains=keyword))
        return OrderInfo.objects.filter(order_id__contains=keyword).all()


    # 1,重写get_serializer方法，返回不同的序列化器

    def get_serializer_class(self):
        if self.action == "retrieve":
            return order_serializers.OrdersModelSerializer

        else:
            return order_serializers.OrderViewSetSerializer

